//Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Diagnostics;

namespace Microsoft.Office.OneNote
{
	public class Page : HierarchyNode
	{
		/// <summary>
		/// Creates a new empty page.
		/// </summary>
		public Page()
		{

		}

		/// <summary>
		/// Creates a new page with the specified name.
		/// </summary>
		/// <param name="name">The name of the page.</param>
		public Page(string name)
		{
			this.Name = name;
		}

		/// <summary>
		/// Clones the Page.
		/// </summary>
		/// <returns>A deep copy of this Page.</returns>
		public override object Clone()
		{
			Page clone = new Page();
			clone.Id = this.Id;

			return clone;
		}

		/// <summary>
		/// Useful for debugging.
		/// </summary>
		/// <returns>A string representation of the Notebook.</returns>
		public override string ToString()
		{
			return this.Name;
		}

		public override void EnsureChildrenSynchronized(bool deep, bool force)
		{
			// TODO: Implement children loading...
			return;
		}

		/// <summary>
		/// Used while committing all pending additions to commit any newly created
		/// node to OneNote.
		/// </summary>
		/// <param name="serializedId">The new ID of this node.</param>
		protected internal override void AddHierarchyToOneNote(out string serializedId)
		{
			Debug.Assert(this.Id == ObjectId.Empty);

			// Create the page via CreateNewPage:
			Application.Server.CreateNewPage(this.ParentHierarchyNode.Id.ToString(), out serializedId);
		}

		/// <summary>
		/// Serializes the <see cref="Page"/> object to Xml in a format
		/// suitable for import into OneNote.
		/// </summary>
		/// <param name="parentNode">
		/// A reference to the parent XML node that we should serialize ourselves
		/// as a child of.
		/// </param>
		/// <param name="deep">
		/// Should we recursively serialize our children?
		/// </param>
		/// <returns>
		/// The serialized node.
		/// </returns>
		protected internal override XmlNode SerializeToXml(XmlNode parentNode, bool deep)
		{
			XmlDocument document = parentNode as XmlDocument;
			if (document == null)
				document = parentNode.OwnerDocument;

			XmlElement serialized = document.CreateElement("Page", OneNote.ApplicationServer.XmlNamespace);
			serialized.SetAttribute("ID", this.Id.ToString());
			serialized.SetAttribute("name", this.Name);
			serialized.SetAttribute("dateTime", XmlConvert.ToString(this.DateTime, XmlDateTimeSerializationMode.RoundtripKind));

			parentNode.AppendChild(serialized);

			if (deep)
			{
				foreach (ANode child in children)
				{
					child.SerializeToXml(serialized, deep);
				}
			}

			return serialized;
		}

		/// <summary>
		/// Deserializes the <see cref="Page"/> object from OneNote's xml 
		/// representation.
		/// </summary>
		/// <param name="node">
		/// A reference to the XML node that we should deserialize ourselves
		/// from.
		/// </param>
		protected internal override void DeserializeFromXml(XmlNode node)
		{
			XmlElement element = (XmlElement) node;

			// Set properties:
			this.Name = element.GetAttribute("name");
			this.DateTime = XmlConvert.ToDateTime(element.GetAttribute("dateTime"), XmlDateTimeSerializationMode.RoundtripKind);

			base.DeserializeFromXml(node);
		}

		public string Name
		{
			get { return GetProperty_Else("name", ""); }
			set { SetProperty("name", value); }
		}

		public DateTime DateTime
		{
			get { return GetProperty_Else("dateTime", DateTime.MinValue); }
			set { SetProperty("dateTime", value); }
		}
	}
}
